package cn.kgc.controller;

import cn.kgc.base.vo.UmsMemberReceiveAddressVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.kgc.entity.UmsMemberReceiveAddress;
import cn.kgc.dto.UmsMemberReceiveAddressAddDTO;
import cn.kgc.dto.UmsMemberReceiveAddressUpdateDTO;
import cn.kgc.service.UmsMemberReceiveAddressService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.kgc.entity.UmsMemberReceiveAddress;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 会员收货地址(UmsMemberReceiveAddress)表控制层
 *
 * @author 石浩杰
 * @since 2023-01-02 10:09:11
 */
@RestController
@RequestMapping("/ums/umsMemberReceiveAddress")
@Api
@Validated
public class UmsMemberReceiveAddressController {
    /**
     * 服务对象
     */
    @Resource
    private UmsMemberReceiveAddressService umsMemberReceiveAddressService;

    @GetMapping("/mylist")
    @ApiOperation(value = "根据用户id获得收货地址",httpMethod = "GET",
            produces = "application/json;charset=utf-8",response = Result.class)
    @ApiImplicitParam(name = "memberId",value = "用户id",dataType = "long",paramType = "query")
    @CrossOrigin
    public List<UmsMemberReceiveAddressVo> getUmsMemberReceiveAddress(@RequestParam(name = "memberId") long memberId){
        LambdaQueryWrapper<UmsMemberReceiveAddress> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UmsMemberReceiveAddress::getMemberId,memberId);
        List<UmsMemberReceiveAddress> list=umsMemberReceiveAddressService.list(lambdaQueryWrapper);
        //将结果转换
        List<UmsMemberReceiveAddressVo> result=list.stream().map(umsMemberReceiveAddress ->{
            UmsMemberReceiveAddressVo umsMemberReceiveAddressVo=new UmsMemberReceiveAddressVo();
            BeanUtils.copyProperties(umsMemberReceiveAddress,umsMemberReceiveAddressVo);
            return umsMemberReceiveAddressVo;
        }).collect(Collectors.toList());
        return result;
    }
    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int")
    @CrossOrigin
    public IPage<UmsMemberReceiveAddress> selectAll(@RequestParam(defaultValue = "1") int current,
                                                    @RequestParam(defaultValue = "5") int pageSize) {
        IPage<UmsMemberReceiveAddress> page = new Page(current, pageSize);

        return umsMemberReceiveAddressService.page(page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @Cacheable(value = "umsMemberReceiveAddress", key = "#id",sync = true)
    public UmsMemberReceiveAddressVo selectOne(@PathVariable Serializable id) {
        UmsMemberReceiveAddressVo umsMemberReceiveAddressVo=new UmsMemberReceiveAddressVo();
        UmsMemberReceiveAddress umsMemberReceiveAddress=umsMemberReceiveAddressService.getById(id);
        BeanUtils.copyProperties(umsMemberReceiveAddress,umsMemberReceiveAddressVo);
        return umsMemberReceiveAddressVo;
    }

    /**
     * 新增数据
     *
     * @param umsMemberReceiveAddressDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean insert(@RequestBody UmsMemberReceiveAddressAddDTO umsMemberReceiveAddressDTO) {
        UmsMemberReceiveAddress umsMemberReceiveAddress = new UmsMemberReceiveAddress();
        BeanUtils.copyProperties(umsMemberReceiveAddressDTO, umsMemberReceiveAddress);
        return this.umsMemberReceiveAddressService.save(umsMemberReceiveAddress);
    }

    /**
     * 修改数据
     *
     * @param umsMemberReceiveAddressDTO 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean update(@RequestBody UmsMemberReceiveAddressUpdateDTO umsMemberReceiveAddressDTO) {
        UmsMemberReceiveAddress umsMemberReceiveAddress = this.umsMemberReceiveAddressService.getById(umsMemberReceiveAddressDTO.getId());
        BeanUtils.copyProperties(umsMemberReceiveAddressDTO, umsMemberReceiveAddress);
        return this.umsMemberReceiveAddressService.updateById(umsMemberReceiveAddress);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Long id) {
        return this.umsMemberReceiveAddressService.removeById(id);
    }
}
